<?php 
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Request;
use think\Session;
use think\Config;
use app\admin\model\Student as StudentM;
use think\Db;
use think\File;

class Signing extends Base
{
    //展示
    
	public  function index()
	{
        $sid = input('get.id');
        //组织查询条件

        $res=db("student")->where('sid',$sid)->find(); //查询新资源
        $zxs = db('admin')->where('id',$res['consultant'])->find();
       
        $tpl_param = array(
            'res'  => $res,
            'zxs'  => $zxs
        );
        $this->assign($tpl_param);
        return  $this->fetch();

    }

    //提交报名记录
    public function submit(){
        $data = \input('post.');
        $idArr = json_decode($data['idArr']);
        $idArr[] = $data['resid'];

        $student = db('student')->where('sid',$data['resid'])->find(); //当前资源

        //添加报名记录
        $Resql = @db('enrollrecord')->insertGetId([
            'resid' => $data['resid'],
            'receipt_number' => $data['receipt'],
            'registeryfee' => $data['registeryFee'],
            'course' => $data['course'],
            'signing_consultant' => $student['consultant'],
            'registerytime' => $data['registeryDate'],
            'create_time' => \time(),
            'update_time' => \time(),
        ]);

        if(!$Resql) $this->error("添加报名记录失败");
        $bind_enroll = $Resql;  //要绑定的报名记录的 id 用于将所有的关系资源绑定到一起
        //修改状态为已签约 添加签约咨询人
        
        $Resql = db('student')->where('sid','in',$idArr)->where('sstatus',0)->update(["signing_consultant"=>$student['consultant'],'bind_enroll'=>$bind_enroll]);
        $Resql = db('student')->where('sid','in',$idArr)->where('sstatus','not in',[0,1])->update(["signing_consultant"=>$student['consultant'],"sstatus"=>1,'bind_enroll'=>$bind_enroll,'status_time'=>date('Y-m-d H:i:s',time())]);
        
        if($Resql<=0) $this->error("修改资源状态失败");
        $this->success("报名完成");
    }


    /**
     * bindenroll 绑定到已有的报名记录
     *  
     * @return void
     */
    public function bindenroll(){
        $is_bind_id = input('?post.bindid');
        if($is_bind_id){
            $bind_id = input('post.bindid'); //要绑定的记录id
            $this_resid = input('post.this_resid');
            $logdata = db('enrollrecord')->where('id',$bind_id)->find();
            $Resql = db('student')->where('sid',$this_resid)->where('sstatus','not in',[0,1])->update(["signing_consultant"=>$logdata['signing_consultant'],"sstatus"=>1,'bind_enroll'=>$bind_id,'status_time'=>date('Y-m-d H:i:s',time())]);

            if($Resql){
                $this->success("已将资源成功绑定到ID为 $bind_id 的记录上");
            }else{
                $this->error('绑定操作失败');
            }
        }

        // 显示匹配的记录
        $sid = input('get.sid');
        $this_res = db('student')->where('sid',$sid)->find();
        $logdata = db('enrollrecord')->order('e.create_time desc')
        ->alias('e')
        ->join('student s', 'e.resid = s.sid')
        ->join('admin a', 'e.signing_consultant = a.id')
        ->field('e.*,a.username,s.sname,s.zxr_tel as stel,s.sadd')
        ->where('s.sname',$this_res['sname'])
        ->select();  //转移日志
        $data = array(  //模板数据
            'logdata'=>$logdata,
            'this_resid'=>$sid,
            'this_res'  =>$this_res
        );
        
        $this->assign($data);
        return  $this->fetch();
    }
    
    /**
     * searchJson 按条件搜索资源
     *
     * @return array
     */
    public function searchJson(){
        $key = input('post.key'); 
        $id  = input('post.thisid');
        $idArr = input('post.id');
        $key = trim($key);//搜索关键字
        $is_key = input('?post.key'); 
        $is_idArr = input('?post.id');
        $student = new StudentM;
        //组织查询条件
        $search = [];
        if($is_key){
            if(!empty($key)) $search = ["sid|sname|stel|zxr_tel|semai|zxr_name"=>['like',"%".$key."%"]];
            $data = $student->where($search)->where('sid','neq',$id)->select();
        }elseif($is_idArr){
            $data = $student->where('sid','in', json_decode($idArr))->select();
            return \json_encode($data);
        }
        
        
        

        return \json_encode($data);
    }
}

 ?>